package wiiusej.hardwareControllers;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import wiiusej.Wiimote;
import wiiusej.apptest.WiimoteEventListener;

public class PermanenSoundController extends PermanentController{

	//Speaker Parameters	
	private static final int THIRD_LEVEL_FREQ = 1000;
	private static final int FIFTH_LEVEL_FREQ = 250;
		
	//queue
	private BlockingQueue<Integer> fromRealTime;
	
	//Sound Freq
	private final int slowFreq = 1000;
	
	public PermanenSoundController(Wiimote wiimote,
			BlockingQueue<Integer> fromRealTime){		
		
		super(wiimote);		
		this.freqMillis = 1250;		
		this.fromRealTime = fromRealTime;
	}
	
	
	@Override
	public void run() {

		while(true){			
			
			try {				
				Integer freq = fromRealTime.poll(slowFreq, TimeUnit.MILLISECONDS);
				
				if(freq != null){
					makeSound(freq);					
				}else{
					myWiimote.sendSound();
				}				
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}						
		}
	}
	
	private void makeSound(Integer freq) {
		
		int i;
		
		switch(freq){
			
		case FIFTH_LEVEL_FREQ:
			
			for(i = 0; i < 2; i++){				
				myWiimote.sendSound();
				try {
					Thread.sleep(75);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			break;
			
		case THIRD_LEVEL_FREQ:			
			for(i = 0; i < 4; i++){				
				myWiimote.sendSound();
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}			
			break;
		
		}
	

}}

